package cn.waka.sys.admin.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.waka.sys.admin.dao.BoxDao;
import cn.waka.sys.admin.object.Page;
import cn.waka.sys.admin.object.Scene;

@Service
public class BoxService {
	
	private BoxDao dao;
	@Autowired
	public void setDao(BoxDao dao){
		this.dao=dao;
	}
	
	public int saveScene(Scene scene){
		
		int rs = 0;
		if(scene.getId()>0){
			rs =dao.update(scene);
		}else{
			rs = dao.saveScene(scene);
		}
		return rs;
	}
	public Page getSceneList(Page p){
		List<Object> list=dao.getSceneList(p);//总列表
		int count=dao.findCount(p);
		p.setData(list);
		p.setCount(count);
		int totalCount=0;
		if(count%p.getSize()>0){
			totalCount=(int) Math.floor(count/p.getSize());//总页数
			totalCount++;
		}else{
			totalCount=count/p.getSize();
		}
		if(totalCount==0){
			totalCount=1;
		}
		p.setTotalPage(totalCount);
		return p;
	}

	public Scene getById(int id){
		return dao.getById(id);
	}
	
	public int update(Scene scene){
		return dao.update(scene);
	}
	
	public int deleteScene(int id){
		return dao.deleteScene(id);
	}
	
	public Page getBoxLogList(int box_id,Page page){
		int start = (page.getPage() - 1) * page.getSize();
		List<Object> list=dao.getBoxLogList(box_id, start, page.getSize());//总列表
		int count=dao.findCount(box_id);
		page.setData(list);
		page.setCount(count);
		int totalCount=0;
		if(count%page.getSize()>0){
			totalCount=(int) Math.floor(count/page.getSize());//总页数
			totalCount++;
		}else{
			totalCount=count/page.getSize();
		}
		if(totalCount==0){
			totalCount=1;
		}
		page.setTotalPage(totalCount);
		return page;
	}
}
